package com.swdegao.iot.netty.controller;

import java.util.List;
import java.util.Set;

import com.swdegao.iot.netty.server.channelhandler.ParseMessgaeHandler;
import com.swdegao.iot.netty.server.guoxinhandler.ConstGuoXin;
import com.swdegao.iot.netty.server.silianhandler.SiLian_Const;
import com.swdegao.iot.netty.udp.server.guoxinudp.GuoXinUDPConst;
import org.apache.http.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.swdegao.iot.netty.entity.ServerResponse;
import com.swdegao.iot.netty.service.CMDService;
import com.swdegao.iot.netty.utils.Const;
import com.swdegao.iot.netty.utils.SendUtil;
import io.netty.channel.Channel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/netty")
public class NettyController {

	@Autowired
	public CMDService cmd;
	// 定义一个全局的记录器，通过LoggerFactory获取
	private final static Logger logger = LoggerFactory.getLogger(NettyController.class);
    /**
     * 获取当前连接数
     * @return 连接个数
     */
    @RequestMapping(value = "/get_channel_size",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<Integer> getChannelSize(){
        return ServerResponse.createBySuccess(Const.getSzie());
    }

    /**
     * 获取连接Id数组
     * @return Id数组
     */
    @RequestMapping(value = "/get_channel_id_list",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<Set<String>> getChannelIDList(String communicationType){
		switch (communicationType){
			case "GuoXin-TCP":
				return ServerResponse.createBySuccess(ConstGuoXin.getIdList());
			case "SiLian-TCP":
				return ServerResponse.createBySuccess(SiLian_Const.getIdList());
			case "DeGao-TCP":
				return ServerResponse.createBySuccess(Const.getIdList());
			case "GuoXin-UDP":
				return ServerResponse.createBySuccess(GuoXinUDPConst.getIdList());
			default:
				return ServerResponse.createBySuccess(Const.getIdList());
		}

    }
    /**
     * 获取设备的连接状态
     * @return Id数组
     */
    @RequestMapping(value = "/get_channel_status",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> getChannelIDStatus(String channelId){
    	String status = Const.getChannelStatus(channelId);
    	if("1".equals(status)){
    		return ServerResponse.createBySuccessOnLine();
    	}else{
    		return ServerResponse.createByErrorNotLine();
    	}

    }


    @RequestMapping(value = "/send_to_channel",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<?> SendToChannel(String channelId,String command){
        SendUtil sendUtil = new SendUtil();
        Channel channel = Const.get(channelId);
        if (channel != null && channel.isActive() && command != null && !"".equals(command)){
        	logger.info("SendToChannel，获取Channel"+channel.toString());
            if (sendUtil.send(channelId,channel,command)){
                return ServerResponse.createBySuccessOnLine();
            }
        }else {
			logger.info("SendToChannel，wei获取Channel"+channel.toString());
		}
        return ServerResponse.createByErrorNotLine();
    }
    /**
	 * 命令下发接口
	 * @param imei
	 * @param data
	 * @param checkflag
	 * @return
	 * @throws Exception
	 * @throws ParseException
	 */
	@ApiOperation(value="命令下发")
	@PostMapping("/commandIssued")
    public JSONObject commandIssued(@ApiParam(name = "imei", value = "表身号")@RequestParam(value="imei", required = false) String  imei,
									@ApiParam(name = "data", value = "数据体")@RequestParam(value="data", required = false) String  data,
									@ApiParam(name = "communicationType", value = "通讯类型")@RequestParam(value="communicationType", required = false) String  communicationType) throws ParseException, Exception {
		JSONObject result = cmd.commandIssued(imei, data,communicationType);
		return result;
    }



//	备份
//	@PostMapping("/commandIssued")
//	public JSONObject commandIssued(@ApiParam(name = "imei", value = "表身号")@RequestParam(value="imei", required = false) String  imei,
//									@ApiParam(name = "data", value = "数据体")@RequestParam(value="data", required = false) String  data) throws ParseException, Exception {
//		JSONObject result = cmd.commandIssued(imei, data);
//		return result;
//	}



	/**
	 * 命令响应接口
	 * @param imei
	 * @param requestID
	 * @return
	 */
	@ApiOperation(value="命令响应")
	@PostMapping("/commandResponse")
	public List<JSONObject> commandResponse(@ApiParam(name = "imei", value = "表身号")@RequestParam(value="imei", required = false) String  imei,
											@ApiParam(name = "requestID", value = "请求ID")@RequestParam(value="requestID", required = false) String  requestID) {
		List<JSONObject> result = cmd.commandResponse(imei, requestID);

		return result;
	}

	 /**
		 * 命令下发接口
		 * @param imei
		 * @param data
		 * @param checkflag
		 * @return
		 * @throws Exception
		 * @throws ParseException
		 */
		@ApiOperation(value="预置命令接受")
		@PostMapping("/waitingPresetCommand")
	    public JSONObject waitingPresetCommand(@ApiParam(name = "imei", value = "表身号")@RequestParam(value="imei", required = false) String  imei,
										@ApiParam(name = "data", value = "数据体")@RequestParam(value="data", required = false) String  data) throws ParseException, Exception {
			JSONObject result = cmd.waitingPresetCommand(imei, data);
			return result;
	    }

	    /**
		 * 命令响应接口
		 * @param imei
		 * @param requestID
		 * @return
		 */
		@ApiOperation(value="预置命令结果查询")
		@PostMapping("/waitingPresetCommandResponse")
		public List<JSONObject> waitingPresetCommandResponse(@ApiParam(name = "imei", value = "表身号")@RequestParam(value="imei", required = false) String  imei,
												@ApiParam(name = "requestID", value = "请求ID")@RequestParam(value="requestID", required = false) String  requestID) {
			List<JSONObject> result = cmd.commandResponse(imei, requestID);

			return result;
		}
}
